package com.ly.live.aliyunlive.controller;


import com.ly.live.aliyunlive.config.AliyunLiveConfig;
import com.ly.live.aliyunlive.model.AjaxResult;
import com.ly.live.aliyunlive.service.LiveCallbackServiceImpl;
import com.ly.live.aliyunlive.util.AliyunLiveUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/aliyunLive")
public class AliyunLiveController {
    @Autowired
    private AliyunLiveConfig aliyunLiveConfig;

    @Autowired
    private LiveCallbackServiceImpl liveCallbackService;

    @RequestMapping("ossCallback")
    @ResponseBody
    public AjaxResult ossCallback(@RequestBody Map<String, Object> paramsMap, HttpServletRequest request){

        System.out.println(request.getRequestURL());
        System.out.println(request.getRequestURI());
        System.out.println(paramsMap.toString());
        System.out.println(request.getParameterMap().entrySet().toArray().toString());
        AjaxResult ajax = AjaxResult.success();
        liveCallbackService.saveOssCallback(paramsMap);
        return ajax;
    }

    @GetMapping("pushLive")
    @ResponseBody
    public AjaxResult addLive(String courseId) {

        /**
         * 注意，推流要在播流域名里面生成
         */


        String pushUrl = AliyunLiveUtil.createPushUrl(courseId, aliyunLiveConfig);

        AjaxResult ajax = AjaxResult.success();
        ajax.put("pushUrl", pushUrl);
        return ajax;
    }

    @GetMapping("pullLive")
    @ResponseBody
    public AjaxResult getLive(String courseId) {

        /**
         * 注意，推流要在播流域名里面生成
         */

        Map<String, String> pullUrl = AliyunLiveUtil.createPullUrl(courseId,  aliyunLiveConfig);

        AjaxResult ajax = AjaxResult.success();
        ajax.put("pullUrl", pullUrl);
        return ajax;
    }

}
